package com.college_career_system.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.college_career_system.dao.UserMapper;
import com.college_career_system.pojo.po.Resume;
import com.college_career_system.dao.ResumeMapper;
import com.college_career_system.pojo.po.User;
import com.college_career_system.service.ResumeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.college_career_system.utils.ResponseCode;
import com.college_career_system.utils.ResponseResult;
import com.college_career_system.utils.TokenUtil;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Empty
 * @since 2022-12-04
 */
@Service
public class ResumeServiceImpl extends ServiceImpl<ResumeMapper, Resume> implements ResumeService {

    private final ResumeMapper resumeMapper;
    private final UserMapper userMapper;
    private final String regex = "[1][0-9]{10}";

    public ResumeServiceImpl(ResumeMapper resumeMapper, UserMapper userMapper) {
        this.resumeMapper = resumeMapper;
        this.userMapper = userMapper;
    }


    /**
     * 添加简历
     * @param resume
     * @return
     */
    @Override
    public JSONObject addResume(HttpServletRequest request, Resume resume) {

        if (resume.getUname() == null || resume.getSex() == null || resume.getAge() == null || resume.getTelephone() == null ||
                !resume.getTelephone().matches(regex)){
            return ResponseResult.failure(ResponseCode.STATUS_ERROR);
        }
        if (resumeMapper.insert(resume) > 0){
            userMapper.changeResumeId(TokenUtil.getId(request),resume.getResumeId());
            return ResponseResult.success("添加成功",ResponseCode.STATUS_SUCCESS);
        }
        return ResponseResult.failure(ResponseCode.STATUS_ERROR);
    }

    @Override
    public JSONObject selectResumeByUid(HttpServletRequest request) {
        return ResponseResult.success(resumeMapper.selectById(userMapper.selectResumeIdByUid(TokenUtil.getId(request))));
    }

    @Override
    public JSONObject changeResumeById(Resume resume) {
        if (resume == null || resume.getResumeId() == null || resume.getUname() == null || resume.getSex() == null ||
                resume.getAge() == null || resume.getTelephone() == null || !resume.getTelephone().matches(regex)){
            return ResponseResult.failure(ResponseCode.STATUS_ERROR);
        }
        QueryWrapper<Resume> wrapper = new QueryWrapper<>();
        wrapper.eq("resume_id",resume.getResumeId());
        if (resumeMapper.update(resume,wrapper) > 0) {
            return ResponseResult.success();
        }
        return ResponseResult.failure(ResponseCode.STATUS_ERROR);
    }
}
